package com.lifl.b328.garden.model;

import java.io.File;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class EditionNoteActivity extends Activity implements Vue {
	private static final int ACTION_TAKE_PICTURE = 1;

	protected Note note;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ajouternote);
		Intent intent = getIntent();
		note = (Note) intent.getExtras().getSerializable(Constantes.INTENT_NOTE);
		getLocation();
		refreshView();
	}
		
	public void onCancelButton(View v){
		setResult(RESULT_CANCELED, new Intent());
		finish();
	}
	public void onValidateButton(View v){
		Intent intent = new Intent();
		
		TextView tv = (TextView) findViewById(R.id.content);
		note.addContent(Constantes.KEY_NOTE_CONTENT, new TextContent(tv.getText().toString()));
		System.out.println(note.getContents().size()+ " taille");
		intent.putExtra("note", note);
		setResult(Activity.RESULT_OK, intent);
		finish();
	}
	
	public void onPhotoPress(View v){
		//detruit l'image de la photo si une est presente
		ImageView iv = (ImageView) findViewById(R.id.photo_add);
		Bitmap bm = iv.getDrawingCache();
		if(bm != null)
			bm.recycle();
		MonLog.print("on photo press", this.getClass());
		Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		File photoTmp = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM), (new Date().getTime())+".jpg");
		Uri uri = Uri.fromFile(photoTmp);
		PhotoContent pc = (PhotoContent) note.getContentByKey(Constantes.NOTE_PHOTO);
		if(pc == null)
			note.addContent(Constantes.NOTE_PHOTO, new PhotoContent(uri.getPath()));
		else
			pc.contenu = uri.getPath();
		photoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
		startActivityForResult(photoIntent,ACTION_TAKE_PICTURE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode){
		case ACTION_TAKE_PICTURE :
			refreshView();
			break;
		}
	}

	@Override
	public void refreshView() {
		TextView tv = (TextView) findViewById(R.id.content);
		String desc = ((TextContent) note.getContentByKey(Constantes.KEY_NOTE_CONTENT)).getText();
		tv.setText(desc);
		PhotoContent pc = (PhotoContent) note.getContentByKey(Constantes.NOTE_PHOTO);
		if(pc != null){
			Bitmap bitmap = pc.getImage();
			ImageView iv = (ImageView) findViewById(R.id.photo_add);
			iv.setImageBitmap(bitmap);
		}
	}
	

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable("note", note);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		this.note = (Note) savedInstanceState.getSerializable("note");
	}
	
	protected void getLocation(){
		LocationManager objgps = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
		criteria.setCostAllowed(false);
		criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
		
		String bestProvider = objgps.getBestProvider(criteria, true);
		System.out.println("best provider =" + bestProvider);
		Location location = objgps.getLastKnownLocation(bestProvider);
		if(location == null){
			System.out.println("hmm");
			return;
		}
		System.out.println(location.getLongitude() + " okok");
		note.addContent(Constantes.NOTE_GPS, 
				new CoordonneesContent(location.getLatitude(), location.getLongitude()));
	}
	
}
